package com.xxh.mqstudy.contrller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class testController {

    @Autowired
    RabbitTemplate rabbitTemplate;


    /**
     * 测试direct模式
     * @return
     */
    @RequestMapping("/sendDirect")
    public String SendDirectMessage(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("mydirect","directkey",map);
        return "direct ok";
    }

    /**
     * 测试fanout模式
     * @return
     */
    @RequestMapping("/sendFanout")
    public String sendFanout(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "都给我听好了!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("myfanoutExchange","",map);
        return "fanout ok";
    }

    @RequestMapping("/sendTopic")
    public String sendTopic(){

        /**
         * 参数1 交换机名字
         * 参数2 路由key
         * 参数3 消息内容
         */
        rabbitTemplate.convertAndSend("mytopicExchange","test.key1","我发个消息，路由key是test.key1");
        rabbitTemplate.convertAndSend("mytopicExchange","test.key2","我发个消息，路由key是test.key2");
        rabbitTemplate.convertAndSend("mytopicExchange","test.key3","我发个消息，路由key是test.key3");
        return "topic ok";
    }

    @RequestMapping("/sendforNo")
    public String SendforPlay(){
        rabbitTemplate.convertAndSend("不存在的exchange","不存在的key","你收的到消息才有鬼");
        return "hehe";
    }

    @RequestMapping("/testMsgACK")
    public String test(){
        rabbitTemplate.convertAndSend("existNoQueueExchange","不存在的key","你收的到消息才有鬼");
        return "hehe2";
    }

}
